/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#ifndef EXAMPLES_ACTIVATION_SOFTMAXFLASH_CUSTOM_TILING_COMPUTE_H
#define EXAMPLES_ACTIVATION_SOFTMAXFLASH_CUSTOM_TILING_COMPUTE_H
#include "../kernel_impl/softmaxflash_custom_tiling.h"
#include "register/op_def_registry.h"
#include "tiling/tiling_api.h"

namespace SoftmaxflashCustomTiling {
constexpr uint32_t SHARED_TMP_BUFFER_SIZE = 61440; // reserved tmpbuffer 60K for softmax compute

struct SingleCoreLoopParam {
    uint32_t singleLoopCoreRowNum{ 0 };  // row num processed in single loop
    uint32_t singleCoreLoopCount{ 0 };   // loop count in single loop
    uint32_t singleCoreLoopTail{ 0 };    // row num of last loop in single core
    uint32_t splitK{ 0 };                // single split num in k direction
    uint32_t loopK{ 0 };                 // loop count in k direction
    uint32_t tailK{ 0 };                 // tail num in k direction
};

SingleCoreLoopParam GetSingleCoreLoopParam(const uint32_t colNum, const uint32_t coreRowNum)
{
    //  Determine the params of single core based on the reduce axis length
    SingleCoreLoopParam singleCoreLoopParam;
    if (colNum >= 1024) {  // reduce axis length >= 1024, set slice factor to 8
        singleCoreLoopParam.splitK = 1024;
        singleCoreLoopParam.loopK = colNum / 1024;
        singleCoreLoopParam.tailK = colNum % 1024;
        singleCoreLoopParam.singleLoopCoreRowNum = 8;
        singleCoreLoopParam.singleCoreLoopCount = coreRowNum / 8;
        singleCoreLoopParam.singleCoreLoopTail = coreRowNum % 8;
    } else if (colNum >= 512) {  // reduce axis length < 1024 && >= 512, set slice factor to 16
        singleCoreLoopParam.splitK = 512;
        singleCoreLoopParam.loopK = colNum / 512;
        singleCoreLoopParam.tailK = colNum % 512;
        singleCoreLoopParam.singleLoopCoreRowNum = 16;
        singleCoreLoopParam.singleCoreLoopCount = coreRowNum / 16;
        singleCoreLoopParam.singleCoreLoopTail = coreRowNum % 16;
    } else if (colNum >= 128) {  // reduce axis length < 512 && >= 128, set slice factor to 32
        singleCoreLoopParam.splitK = 128;
        singleCoreLoopParam.loopK = colNum / 128;
        singleCoreLoopParam.tailK = colNum % 128;
        singleCoreLoopParam.singleLoopCoreRowNum = 32;
        singleCoreLoopParam.singleCoreLoopCount = coreRowNum / 32;
        singleCoreLoopParam.singleCoreLoopTail = coreRowNum % 32;
    } else {  // reduce axis length < 128 && >= 0, set slice factor to 64
        singleCoreLoopParam.splitK = colNum;
        singleCoreLoopParam.loopK = 1;
        singleCoreLoopParam.tailK = 0;
        singleCoreLoopParam.singleLoopCoreRowNum = 64;
        singleCoreLoopParam.singleCoreLoopCount = coreRowNum / 64;
        singleCoreLoopParam.singleCoreLoopTail = coreRowNum % 64;
    }
    return singleCoreLoopParam;
}

void ComputeTiling(const uint32_t rowNum, const uint32_t colNum, const uint32_t coreNum,
    SoftmaxflashCustomTilingData* tiling)
{
    uint32_t localworkspaceSize = SHARED_TMP_BUFFER_SIZE;

    auto alignedRowNum = (rowNum + coreNum - 1) / coreNum * coreNum;
    auto coreRowNum = alignedRowNum / coreNum;  // each core equal distribution
    auto tailCoreRowNum = rowNum % coreRowNum;  // last core process the tail rownum
    auto usedBlockDim = rowNum / coreRowNum;    // the core num used actually

    SingleCoreLoopParam mainCoreLoopParam = GetSingleCoreLoopParam(colNum, coreRowNum);
    SingleCoreLoopParam tailCoreLoopParam;
    if (usedBlockDim == coreNum && tailCoreRowNum == 0) {
        tailCoreLoopParam = GetSingleCoreLoopParam(colNum, coreRowNum);
    } else {
        tailCoreLoopParam = GetSingleCoreLoopParam(colNum, tailCoreRowNum);
    }

    ge::Shape softmaxComputeShape({ mainCoreLoopParam.singleLoopCoreRowNum, mainCoreLoopParam.splitK });
    uint32_t apiNeedMinTmpSize = 0;
    if (mainCoreLoopParam.singleLoopCoreRowNum % 8 == 0 && mainCoreLoopParam.splitK % 64 == 0) { // enable basicBlock
        apiNeedMinTmpSize =
            AscendC::GetSoftMaxFlashV2MinTmpSize(softmaxComputeShape, sizeof(float), sizeof(float), true, true);
    } else {
        apiNeedMinTmpSize =
            AscendC::GetSoftMaxFlashV2MinTmpSize(softmaxComputeShape, sizeof(float), sizeof(float), true, false);
    }
    if (apiNeedMinTmpSize > SHARED_TMP_BUFFER_SIZE) {
        localworkspaceSize = apiNeedMinTmpSize;
    } else {
        localworkspaceSize = SHARED_TMP_BUFFER_SIZE;
    }
    // get SoftMax Tiling
    if (mainCoreLoopParam.singleLoopCoreRowNum % 8 == 0 && mainCoreLoopParam.splitK % 64 == 0) { // enable basicBlock
        AscendC::SoftMaxFlashV2TilingFunc(softmaxComputeShape, sizeof(float), sizeof(float), localworkspaceSize,
            tiling->softmaxTilingData, true, true);
    } else {
        AscendC::SoftMaxFlashV2TilingFunc(softmaxComputeShape, sizeof(float), sizeof(float), localworkspaceSize,
            tiling->softmaxTilingData, true, false);
    }

    tiling->columnLength = colNum;
    tiling->rowLength = rowNum;
    tiling->sharedTmpBufferSize = localworkspaceSize;
    tiling->usedBlockDim = usedBlockDim;
    tiling->coreRowNum = coreRowNum;
    tiling->tailCoreRowNum = tailCoreRowNum;
    tiling->splitK = mainCoreLoopParam.splitK;
    tiling->loopK = mainCoreLoopParam.loopK;
    tiling->tailK = mainCoreLoopParam.tailK;

    tiling->singleLoopCoreRowNum = mainCoreLoopParam.singleLoopCoreRowNum;
    tiling->singleCoreLoopCount = mainCoreLoopParam.singleCoreLoopCount;
    tiling->singleCoreLoopTail = mainCoreLoopParam.singleCoreLoopTail;
    tiling->tailCoreSingleLoopCoreRowNum = tailCoreLoopParam.singleLoopCoreRowNum;
    tiling->tailCoreSingleCoreLoopCount = tailCoreLoopParam.singleCoreLoopCount;
    tiling->tailCoreSingleCoreLoopTail = tailCoreLoopParam.singleCoreLoopTail;
}
}
#endif // EXAMPLES_ACTIVATION_SOFTMAXFLASH_CUSTOM_TILING_COMPUTE_H